﻿using Dapper;
using ERPEF.Model;
using ERPService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Data;

namespace S4Project.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InvGoodsInventoryController : ControllerBase
    {
        private readonly inv_goods_inventoryService inv_goods_inventory;
        private readonly MyDbContext myDbContext;
        public InvGoodsInventoryController(MyDbContext myDb, inv_goods_inventoryService inv_goods_inventoryService)
        {
            myDbContext = myDb;
            inv_goods_inventory = inv_goods_inventoryService;
        }

        //库存查询
        [HttpGet]
        public async Task<IActionResult> Query()
        {
            var warehouse = await inv_goods_inventory.Query();
            if (warehouse != null)
            {
                return Ok(warehouse);
            }
            else
            {
                return NotFound("404");
            }
        }
        //根据物品名称查询仓库和仓库数量
        //[HttpGet]
        //public async Task<IActionResult> QueryByWarehouse(string igi_name)
        //{

        //    var inventories = await myDbContext.inv_goods_inventory
        //.Where(i => i.igi_name.Equals(igi_name))
        //.ToListAsync();

        //    var results = new List<object>();
        //    foreach (var inventory in inventories)
        //    {
        //        var quantity = inventory.igi_quantity;
        //        results.Add(new { Warehouse = inventory.igi_warehouse, Quantity = quantity });
        //    }

        //    return Ok(results);
        //}

        //通过名称查询仓库
        [HttpGet]
        public async Task<IActionResult> QueryWarehouseByName(string igi_name)
        {
            var inventories = await myDbContext.inv_goods_inventory
                .Where(i => i.igi_name.Equals(igi_name))
                .Select(i => new { igi_warehouse = i.igi_warehouse })
                .ToListAsync();

            return Ok(inventories);
        }


        [HttpGet]
        public async Task<IActionResult> QueryInventoryQuantityByProductAndWarehouse(string igi_name, string igi_warehouse)
        {
            var inventory = await myDbContext.inv_goods_inventory
                .Where(i => i.igi_name.Equals(igi_name) && i.igi_warehouse.Equals(igi_warehouse))
                .Select(i => new { igi_quantity = i.igi_quantity })
                .FirstOrDefaultAsync();

            return Ok(inventory);
        }

        //库存分页
        [HttpGet]
        public ActionResult<FenYe<inv_goods_inventory>> GetFenYe(int currentPage, int pageSize, string where)
        {
            using (var conn = myDbContext.Database.GetDbConnection())
            {
                conn.Open();
                var parameters = new DynamicParameters();
                parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
                parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "igi_id"); // 更改为正确的列名
                parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "inv_goods_inventory"); // 更改为正确的表名
                parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
                parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: where); // 添加 where 参数
                parameters.Add("row", dbType: DbType.Int32, direction: ParameterDirection.Output); // 修改为和存储过程中一致的参数名
                string sql = "[dbo].[FenYe]";
                var result = conn.Query<inv_goods_inventory>(sql, parameters, commandType: CommandType.StoredProcedure);
                int row = parameters.Get<int>("row");
                FenYe<inv_goods_inventory> fenYe = new FenYe<inv_goods_inventory>()
                {
                    GoodInventoryData = result,
                    Rows = row,
                };

                if (fenYe.GoodInventoryData.Any())
                {
                    return fenYe;
                }
                else
                {
                    return Content("没有查到数据");
                }
            }
        }



    }
}
